package com.cyandpos.pad.activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.TextView;
import android.widget.Toast;

import com.changyi.device.APApplication;
import com.cyandpos.pad.R;
import com.cyandpos.pad.common.ManagerCardManager.MangerCardData;
import com.cyandpos.pad.common.commondata.BaseResponse;
import com.cyandpos.pad.common.commondata.Constant;
import com.cyandpos.pad.common.commondata.ErpCommonData;
import com.cyandpos.pad.common.commondata.MainSaleConstant;
import com.cyandpos.pad.common.commondata.SktConfigConstant;
import com.cyandpos.pad.common.netmanager.PosHttpManager;
import com.cyandpos.pad.http.OkHttpUtils;
import com.cyandpos.pad.morefunction.CashCollectionPresenter;
import com.cyandpos.pad.morefunction.ICashMainSaleView;
import com.cyandpos.pad.morefunction.PayGridMainSaleAdapter;
import com.cyandpos.pad.morefunction.PayGridView;
import com.cyandpos.pad.pickhangup.PickHangUpTrade;
import com.cyandpos.pad.posdata.OrderNoData;
import com.cyandpos.pad.presentation.AdPresentationUtil;
import com.cyandpos.pad.utils.PermissionsUtils;
import com.cyandpos.pad.utils.ToastUtils;
import com.google.gson.JsonObject;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MorefunctionActivity extends Activity implements ICashMainSaleView, View.OnClickListener {
    private AlertDialog mDialog;
    private PayGridMainSaleAdapter payAdapter;
    @BindView(R.id.tv_dialog_title_name)
    TextView tvName;
    @BindView(R.id.gv_act_sale)
    PayGridView gv_act_sale;
    private ProgressDialog mProgressDialog;//等待框

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_morefunction);
        APApplication.getInstance().addActivity(this);
        new AdPresentationUtil().initDisplayData(this);
        ButterKnife.bind(this);
        initView();
        initData();

     }

    @Override
    protected void onResume() {
        super.onResume();
        tvName.setText("功能选择");
    }

    private void initView() {
        tvName.setOnClickListener(this);
       // setActivityDialogSize();
        mProgressDialog = new ProgressDialog(this);
        mProgressDialog.setCancelable(false);
     }
    private void setActivityDialogSize() {
        Display mDisplay = getWindowManager().getDefaultDisplay();
        DisplayMetrics outMetrics = new DisplayMetrics();
        mDisplay.getMetrics(outMetrics);
        WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
        layoutParams.width = (int) (outMetrics.widthPixels * 0.5);
        //设置高度会导致弹框无法居中
        layoutParams.height = (int) (outMetrics.heightPixels * 0.5);
        getWindow().setAttributes(layoutParams);
    }
    private void initData() {
        getVisLayout();
    }
    public void getVisLayout(){
        //  服务 有返回数据后  放开即可
        PermissionsUtils.visPermission(null,this,SktConfigConstant.VIS_MainSale, SktConfigConstant.MainSale_ALL, new PermissionsUtils.PermissionListener() {
            @Override
            public void onPermissionSuccess(MangerCardData mangerCardData, int flag) {
                String name = mangerCardData.getName();
                String[] split = name.split(";");
                new CashCollectionPresenter(MorefunctionActivity.this).getnMainSaleList(split);
            }
            @Override
            public void onPermissionFaild(String msg) {
                ToastUtils.showMsg("缺少功能权限"+msg);

            }
        });
    }
    @Override
    public void showPaymetListData(List<Integer> imgs, List<Integer> select_imgs, final List<String> names) {
        payAdapter = new PayGridMainSaleAdapter(MorefunctionActivity.this, imgs, select_imgs, names);
        gv_act_sale.setAdapter(payAdapter);
        gv_act_sale.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                switch (names.get(i)){
                    case MainSaleConstant.MAIN_TIH_NAME: {//提货
                    }
                    break;
                    case MainSaleConstant.MAIN_CDXP_NAME: {// 重打小票
                        PermissionsUtils.checkPermission(null,MorefunctionActivity.this, PermissionsUtils.REPRINT, new PermissionsUtils.PermissionListener() {
                            @Override
                            public void onPermissionSuccess(MangerCardData mangerCardData, int flag) {
                                Intent intent= new Intent(MorefunctionActivity.this,ReprintTicketActivity.class);
                                startActivity(intent);
                                finish();
                            }

                            @Override
                            public void onPermissionFaild(String msg) {
                                ToastUtils.showMsg(msg);
                            }
                        });
                    }
                    break;
                    case MainSaleConstant.MAIN_DZ_NAME: { // 班报日报
                        PermissionsUtils.checkPermission(null,MorefunctionActivity.this, PermissionsUtils.REPORT, new PermissionsUtils.PermissionListener() {
                            @Override
                            public void onPermissionSuccess(MangerCardData mangerCardData, int flag) {
                                Intent intent= new Intent(MorefunctionActivity.this,ReportActivity.class);
                                startActivity(intent);
                                finish();
                            }
                            @Override
                            public void onPermissionFaild(String msg) {
                                ToastUtils.showMsg(msg);
                            }
                        });
                    }
                    break;
                    case MainSaleConstant.MAIN_YHGN_NAME: {// 银行功能
                        PermissionsUtils.checkPermission(null,MorefunctionActivity.this, PermissionsUtils.BANK, new PermissionsUtils.PermissionListener() {
                            @Override
                            public void onPermissionSuccess(MangerCardData mangerCardData, int flag) {
                                Intent intent= new Intent(MorefunctionActivity.this,BankPayActivity.class);
                                startActivity(intent);
                                finish();
                            }
                            @Override
                            public void onPermissionFaild(String msg) {
                                ToastUtils.showMsg(msg);
                            }
                        });
                    }
                    break;
                    case MainSaleConstant.MAIN_HYGN_NAME: {// 会员功能
                        setResult(Constant.MORE_FUNCTION_TO_MAIN_ADD_MEMBER_RETID, new Intent());
                        finish();
                    }
                    break;
                    case MainSaleConstant.MAIN_SPCX_NAME: {// 商品查询
                        Intent intent= new Intent(MorefunctionActivity.this,QueryGoodsActivity.class);
                        startActivity(intent);
                        finish();
                    }
                    break;
                    case MainSaleConstant.MAIN_TUIH_NAME: {// 退货
                        PermissionsUtils.checkPermission(null,MorefunctionActivity.this, PermissionsUtils.XSTH, new PermissionsUtils.PermissionListener() {
                            @Override
                            public void onPermissionSuccess(MangerCardData mangerCardData, int flag) {
                                Intent intent= new Intent(MorefunctionActivity.this,RefundDialog.class);
                                startActivity(intent);
                                finish();
                            }

                            @Override
                            public void onPermissionFaild(String msg) {
                                ToastUtils.showMsg(msg);
                            }
                        });
                    }
                    break;
                    case MainSaleConstant.MAIN_ZDQX_NAME: {// 整单取消
                        PermissionsUtils.checkPermission(null,MorefunctionActivity.this, PermissionsUtils.CANCEL, new PermissionsUtils.PermissionListener() {
                            @Override
                            public void onPermissionSuccess(MangerCardData mangerCardData, int flag) {
                                setResult(Constant.MORE_FUNCTION_TO_MAIN_CANCEL_TRADE_RETID, new Intent());
                                finish();
                            }

                            @Override
                            public void onPermissionFaild(String msg) {
                                ToastUtils.showMsg(msg);
                            }
                        });
                    }
                    break;
                    case MainSaleConstant.MAIN_DDCX_NAME: {// 订单查询
                        Intent intent =new Intent(MorefunctionActivity.this,OrderqueryActivity.class);
                        startActivity(intent);
                        finish();
                    }
                    break;
                    case MainSaleConstant.MAIN_ZDZK_NAME: {// 整单折扣
                        PermissionsUtils.checkPermission(null,MorefunctionActivity.this, PermissionsUtils.SPZK, new PermissionsUtils.PermissionListener() {
                            @Override
                            public void onPermissionSuccess(MangerCardData mangerCardData, int flag) {
                                setResult(Constant.MORE_FUNCTION_TO_MAIN_FULL_ORDER_DISCOUNT, new Intent());
                                finish();
                            }

                            @Override
                            public void onPermissionFaild(String msg) {
                                ToastUtils.showMsg(msg);
                            }
                        });
                    }
                    break;

                    case MainSaleConstant.MAIN_FJCX_NAME: {//附加查询
                        Intent intent =new Intent(MorefunctionActivity.this,FjcxActivity.class);
                        startActivity(intent);
                        finish();
                    }
                    break;

                    case MainSaleConstant.MAIN_SP_NAME: {//锁屏
                        startActivity(new Intent(MorefunctionActivity.this, LockActivity.class));
                        finish();
                    }
                    break;

                    case MainSaleConstant.MAIN_XGKL_NAME: {//修改口令
                        PermissionsUtils.checkPermission(null,MorefunctionActivity.this, PermissionsUtils.PASSWORD, new PermissionsUtils.PermissionListener() {
                            @Override
                            public void onPermissionSuccess(MangerCardData mangerCardData, int flag) {
                                startActivity(new Intent(MorefunctionActivity.this, ModifyPasswordActivity.class));
                                finish();
                            }

                            @Override
                            public void onPermissionFaild(String msg) {
                                ToastUtils.showMsg(msg);
                            }
                        });
                    }
                    break;
                    case MainSaleConstant.MAIN_GD_NAME: {//挂单
                        setResult(Constant.MORE_FUNCTION_TO_MAIN_SUSPEND_ORDER, new Intent());
                        finish();
                    }
                    break;

                    case MainSaleConstant.MAIN_QD_NAME: {//取单
                        showPickHangUpDialog();
                    }
                    break;

                    case MainSaleConstant.MAIN_FSTH_NAME: {//负数退货
                        PermissionsUtils.visPermission(null,MorefunctionActivity.this,SktConfigConstant.VIS_FSXS, SktConfigConstant.VIS_FSXSTH,
                                new PermissionsUtils.PermissionListener() {
                                    @Override
                                    public void onPermissionSuccess(MangerCardData mangerCardData, int flag) {
                                        String name = mangerCardData.getName();
                                        if (name.equals("0")){ // 0是不允许退货 1是允许退货
                                            Toast.makeText(MorefunctionActivity.this,"此款台不允许负数退货",Toast.LENGTH_SHORT).show();
                                        }else {
                                            setResult(Constant.MORE_FUNCTION_TO_MAIN_NEGATIVE_RETRUN, new Intent());
                                            finish();
                                        }

                                    }

                                    @Override
                                    public void onPermissionFaild(String msg) {
                                        ToastUtils.show(msg);
                                    }
                                });
                    }
                    break;

                    case MainSaleConstant.MAIN_TJXP_NAME: {//添加小票
                        setResult(Constant.MORE_FUNCTION_TO_MAIN_ADD_SUB_TICKET, new Intent());
                        finish();
                    }
                    break;
                    case MainSaleConstant.MAIN_DKQX_NAME: {//打开钱箱
                        Intent intent =new Intent(MorefunctionActivity.this,OpenCashboxActivity.class);
                        startActivity(intent);
                        finish();
                    }
                    break;
                    case MainSaleConstant.MAIN_DJD_NAME: {//订金单
                       /* Intent intent =new Intent(MorefunctionActivity.this,DepositActivity.class);
                        startActivity(intent);
                        finish();*/
                        djdfunction();
                    }
                    break;
                    case MainSaleConstant.MAIN_FY_NAME: {//关于
                        Intent intent =new Intent(MorefunctionActivity.this,AboutActivity.class);
                        startActivity(intent);
                        finish();
                    }

                    break;
                    case MainSaleConstant.MAIN_YD_NAME: {//餐饮预定
                        Intent intent =new Intent(MorefunctionActivity.this,OrderFoodActivity.class);
                        startActivity(intent);
                        finish();
                    }
                    break;
                    case MainSaleConstant.MAIN_QZ_NAME: {//餐饮清桌
                        Intent intent =new Intent(MorefunctionActivity.this,ClearTableActivity.class);
                        startActivity(intent);
                        finish();

                    }
                    break;
                    case MainSaleConstant.MAIN_YDXQ_NAME: {//预定详情
                        Intent intent =new Intent(MorefunctionActivity.this,OrderFoodInfoActivity.class);
                        startActivity(intent);
                        finish();
                    }
                    break;
                    case MainSaleConstant.MAIN_OFFLINE_UPLOAD_NAME: {//脱机上传
                        Intent intent =new Intent(MorefunctionActivity.this,OfflineUploadDataActivity.class);
                        startActivity(intent);
                        finish();
                    }
                    break;
                    case MainSaleConstant.MAIN_OFFLINE_QUREY_NAME: {//数据库查询
                        Intent intent =new Intent(MorefunctionActivity.this,OfflineDbManagerActivity.class);
                        startActivity(intent);
                        finish();
                    }
                    break;
                    case MainSaleConstant.MAIN_PRINT_FLOWER_NAME: //印花
                        setResult(Constant.MORE_FUNCTION_TO_MAIN_PRINT_FLOWER, new Intent());
                        finish();
                        break;
                    case MainSaleConstant.MAIN_REGIST_MENBER_NAME: {// 会员注册
                        setResult(Constant.MORE_FUNCTION_TO_MAIN_REGISTER_MEMBER_RETID, new Intent());
                        finish();
                    }
                    break;
                    default:
                        break;
                }
            }
        });
    }

    /**
     * 取单
     */
    private void showPickHangUpDialog() {
        PickHangUpTrade pick = new PickHangUpTrade(MorefunctionActivity.this, new PickHangUpTrade.IOnPickListener() {
            @Override
            public void onPickClick() {
                setResult(Constant.MORE_FUNCTION_TO_MAIN_PICK_HANGUP_TRADE, new Intent());
                finish();
            }
        });
        pick.show();
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tv_dialog_title_name:  //dialog关闭
                finish();

                break;

            case R.id.btn_main_other_funtion:

                break;
        }
    }

    public void  djdfunction(){
        if(ErpCommonData.ticketInfo.getTicketGoodCount()>0){
            ToastUtils.show("请先点击订金单,然后再录入商品!");
        }else{
            ErpCommonData.ticketInfo.setDJD(true);
            ErpCommonData.ticketInfo.setFSTH(false);

            mProgressDialog.setMessage("更新单号信息中……");
            mProgressDialog.show();
            PosHttpManager.CreateSaleId("","","", new OkHttpUtils.ResultCallback() {
                @Override
                public void onSuccess(JsonObject response) {
                    BaseResponse<OrderNoData> baseResponse = BaseResponse.fromJson(response.toString(),OrderNoData.class);
                    mProgressDialog.dismiss();
                    if(baseResponse.getCode().equals("0")){
                        String orderNo = baseResponse.getData().getOrderNo();
                        ErpCommonData.orderNo = orderNo;
                        ErpCommonData.ticketInfo.setOrderNo(orderNo);
                        finish();
                    }else {
                        if (TextUtils.isEmpty(baseResponse.getTips())){
                            ToastUtils.show(baseResponse.getMsg());
                        }else {
                            ToastUtils.show(baseResponse.getTips());
                        }
                        ErpCommonData.ticketInfo.setDJD(false);
                        ErpCommonData.ticketInfo.setFSTH(false);
//                                  ErpCommonData.isDJD = false;
//                                  ErpCommonData.isFSTH=false;
                        finish();
                    }
                }

                @Override
                public void onFailure(String msg) {
                    mProgressDialog.dismiss();
                    ToastUtils.show(msg);
                    ErpCommonData.ticketInfo.setDJD(false);
                    ErpCommonData.ticketInfo.setFSTH(false);
//                              ErpCommonData.isDJD = false;
//                              ErpCommonData.isFSTH=false;
                    finish();
                }
            });
        }

    }
}
